package com.unionwo.service;


import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.unionwo.commons.util.DateUtil;
import com.unionwo.json.UserPackageInfo;
import com.unionwo.model.UnionResponse;
import com.unionwo.model.UserPackageResponse;

/****
 * 获取地理位置信息服务
 * @author admin
 *
 */
@Service("getUserPackageInfoService")
public class GetUserPackageInfoService implements UnionService {

	private Logger log = Logger.getLogger(GetUserPackageInfoService.class);
	
	@Override
	/*****
	 **@param u  userToken
	 **@param n  电话号码
	 **@return UnionResponse
	 */
	public UnionResponse doRequest(HttpServletRequest request) throws Exception {
		String userToken = request.getParameter("u");
		String telephoneNumber = request.getParameter("n");
		log.info("userToken:"+userToken+",telephoneNumber:"+telephoneNumber);
		//获取本月剩余天数
		//int restDay = DateUtil.getRestDayCurrentMonth();
		Calendar c = Calendar.getInstance();
		c.getMaximum(Calendar.DAY_OF_MONTH);
		UserPackageResponse res = null;
		if(res==null){
			res = test();
		}
		
		return res;
	}
	
	public static UserPackageResponse test(){
		int restDay = DateUtil.getRestDayCurrentMonth();
		int array[] = {300,400,500,600,700,800,900,1000};
		Random r = new Random();
		int t1=array[r.nextInt(8)],t2=array[r.nextInt(8)],t3=array[r.nextInt(8)],t4=array[r.nextInt(8)];
		DecimalFormat df=new DecimalFormat("#.##");
		double r1 = t1*Double.valueOf(df.format(r.nextDouble()));
		double r2 = t2*Double.valueOf(df.format(r.nextDouble()));
		double r3 = t3*Double.valueOf(df.format(r.nextDouble()));
		double r4 = t4*Double.valueOf(df.format(r.nextDouble()));
		UserPackageResponse res = new UserPackageResponse();
		res.setResult("ok");
		res.setIc(new UserPackageInfo(r1, t1-r1, r1/restDay));
		res.setIm(new UserPackageInfo(r2, t2-r2, r2/restDay));
		res.setImm(new UserPackageInfo(r3, t3-r3, r3/restDay));
		res.setIu(new UserPackageInfo(r4, t4-r4, r4/restDay));
		return res;
	}
	

}
